/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.service;

import br.com.foxline.clinicplus.entity.Operator;
import br.com.foxline.clinicplus.entity.Procedure;
import br.com.foxline.clinicplus.entity.ValueProcedure;
import br.com.foxline.clinicplus.enums.GroupProcedure;
import br.com.foxline.clinicplus.enums.Reports;
import br.com.foxline.clinicplus.vo.VoProcedureValue;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author roniere
 */
@Stateless
public class ProcedureService extends GenericService<Procedure> {

    @EJB
    ValueProcedureService valueProcedureService;
    @EJB
    ClinicService clinicService;

    public ProcedureService() {
        super(Procedure.class);
    }

    public List<Procedure> findAll() {
        return getEntityManager().createQuery("select p from  Procedure p where p.active = true").getResultList();
    }

    public BigDecimal getValueProcedureOperator(Procedure procedure, Operator operator) {

        try {

            String sql;

            if (operator == null || operator.getId() == null) {
                sql = "Select MAX(vp.amount) from Procedure p JOIN  p.valueProcedure vp where"
                        + " p.id = :procedure and p.active = true";
            } else {
                sql = "Select vp.amount from Procedure p JOIN  p.valueProcedure vp JOIN vp.operator o where"
                        + " p.id = :procedure and o.id = :operator and p.active = true";
            }
            Query query = getEntityManager().createQuery(sql);

            query.setParameter("procedure", procedure.getId());
            if (operator != null && operator.getId() != null) {
                query.setParameter("operator", operator.getId());
            }

            if (!query.getResultList().isEmpty()) {
                return (BigDecimal) query.getResultList().get(0);
            }
            return new BigDecimal("0");
        } catch (Exception e) {
            e.printStackTrace();
            return new BigDecimal("0");
        }

    }

    public List<Procedure> findProcedure(Procedure pro, Boolean active) {
        String sql = "select p from Procedure p WHERE ";
        if (active != null && active) {
            sql += " p.active = true and ";
        }
        if ((pro.getName() != null) && (!pro.getName().equals(""))) {
            sql += " lower(p.name) like lower(:name) and  ";
        }

        if ((pro.getCod() != null) && (!pro.getCod().equals(""))) {
            sql += " lower(p.cod) like lower(:cod) and  ";
        }
        if ((pro.getDuration() != null)) {
            sql += " p.duration =:duration and  ";
        }
        sql += "1=1";

        Query query = getEntityManager().createQuery(sql);

        if ((pro.getName() != null) && (!pro.getName().equals(""))) {
            query.setParameter("name", "%" + pro.getName() + "%");
        }
//        if ((!op.getNumReg().equals("")) && (op.getNumReg() != null)) {
//            query.setParameter("num", "%" + op.getNumReg() + "%");
//        }
        if ((pro.getCod() != null) && (!pro.getCod().equals(""))) {
            query.setParameter("cod", "%" + pro.getCod() + "%");
        }

        return query.getResultList();
    }

    public List<Procedure> findProcedure(Procedure pro, Operator o) {

        String sql = "Select DISTINCT(p) from Procedure p JOIN  p.valueProcedure vp JOIN vp.operator o where p.active = true and ";

        if ((pro.getName() != null) && (!pro.getName().equals(""))) {
            sql += " lower(p.name) like lower(:name) and  ";
        }

        if ((pro.getCod() != null) && (!pro.getCod().equals(""))) {
            sql += " lower(p.cod) like lower(:cod) and  ";
        }

        if (o != null) {
            sql += " o.id = :operator and ";
        }

        sql += "1=1";

        Query query = getEntityManager().createQuery(sql);

        if ((pro.getName() != null) && (!pro.getName().equals(""))) {
            query.setParameter("name", "%" + pro.getName() + "%");
        }

        if ((pro.getCod() != null) && (!pro.getCod().equals(""))) {
            query.setParameter("cod", "%" + pro.getCod() + "%");
        }

        if (o != null) {
            query.setParameter("operator", o.getId());
        }

        return query.getResultList();
    }
    
    public List<Procedure> autocompletePorGrupoProcedimento(String pro, GroupProcedure gp) {
        String sql = "select p from Procedure p WHERE p.active = true and ";

        if ((pro != null) && (!pro.equals(""))) {
            sql += " lower(p.name) like lower(:name) and  ";
        }
        
        if(gp != null){
            sql += " p.groupProcedure = :gp and ";
        }

        sql += "1=1 Order by p.name ASC ";

        Query query = getEntityManager().createQuery(sql);

        if ((pro != null) && (!pro.equals(""))) {
            query.setParameter("name", "%" + pro + "%");
        }
        if(gp != null){
            query.setParameter("gp", gp);
        }
        
        return query.getResultList();

    }

    public List<Procedure> autocomplete(String pro) {
        String sql = "select p from Procedure p WHERE p.active = true and ";

        if ((pro != null) && (!pro.equals(""))) {
            sql += " lower(p.name) like lower(:name) and  ";
        }

        sql += "1=1 Order by p.name ASC ";

        Query query = getEntityManager().createQuery(sql);

        if ((pro != null) && (!pro.equals(""))) {
            query.setParameter("name", "%" + pro + "%");
        }
        
        return query.getResultList();

    }

    public List<Procedure> findProcedureOperator(Operator op) {
        String sql = "select p from Procedure p JOIN p.valueProcedure v JOIN v.operator o "
                + " WHERE o.id = :id and p.active = true";
        System.err.println(sql);
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("id", op.getId());
        return query.getResultList();

    }

    public List<VoProcedureValue> findVoProcedureOperator(Operator op) {
        String sql = "select v from Procedure p JOIN p.valueProcedure v JOIN v.operator o "
                + " WHERE o.id = :id and p.active = true ";

        Query query = getEntityManager().createQuery(sql, VoProcedureValue.class);
        query.setParameter("id", op.getId());
        List<VoProcedureValue> result = new ArrayList<VoProcedureValue>();

        Procedure p;
        for (ValueProcedure vp : (List<ValueProcedure>) query.getResultList()) {
            p = valueProcedureService.getProcedureForId(vp.getId());
            result.add(new VoProcedureValue(vp.getId(), p.getCod(), p.getName(), vp.getAmount()));
        }

        return result;

    }

    public void reportProcedureOperator(Operator operator, List<VoProcedureValue> lstProcedure) {
        HashMap<String, Object> parametros;
        parametros = new HashMap<String, Object>();

        if (lstProcedure == null) {
            lstProcedure = findVoProcedureOperator(operator);
        }

        parametros.put("par_op", operator.getName());
        parametros = clinicService.getCabecalho(parametros);
        ReportService.genReport(parametros, lstProcedure, Reports.PROCEDUREOPERATOR);
    }
}
